package org.jiucai.cas.auth.dao;

import java.util.List;

import org.jiucai.appframework.base.spring.dao.BaseDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import org.jiucai.cas.domain.AuthQuery;
import org.jiucai.cas.domain.UserInfo;

@Repository("authDao")
public class AuthDaoImpl extends BaseDao implements AuthDao {
	
	//用户表表名
	private static final String 	TAB_NAME_USER_INFO	= "demo_user";
	
	/**
	 * 登录限制条件
	 * @return
	 */
	protected String getUserCondition(){
		StringBuffer sql = new StringBuffer();
		
		sql.append(" and u.locked = 0 ").append("\n");
		sql.append(" and u.deleted = 0 ").append("\n");
		
		return sql.toString();
	}

	/**
	 * 检查用户是否存在
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public Boolean checkUser(AuthQuery query) {
		
		if(null == query){
			return false;
		}

		StringBuffer sql = new StringBuffer();

		sql.append(" select ").append("\n");
		sql.append("	count(*) as ct ").append("\n");
		sql.append("  from ").append(TAB_NAME_USER_INFO).append(" u ").append("\n");
		sql.append(" where u.user_id = :userId ").append("\n");
		sql.append(getUserCondition()).append("\n");

		int userCount = getSimpleJdbcTemplate().queryForInt(sql.toString(),
				new BeanPropertySqlParameterSource(query));

		log.debug("userCount " + userCount);

		return userCount > 0 ? true : false;

	}

	/**
	 * 获取用户信息
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public UserInfo getUserInfo(AuthQuery query) {
		
		if(null == query){
			return null;
		}
		
		UserInfo user = null;
		
		StringBuffer sql = new StringBuffer();

		sql.append(" select ").append("\n");
		sql.append("        u.user_id as user_id, ").append("\n");
		sql.append("        u.login_name, ").append("\n");
		sql.append("        u.display_name, ").append("\n");
		sql.append("        u.password ").append("\n");
		sql.append("  from ").append(TAB_NAME_USER_INFO).append(" u ").append("\n");
		sql.append(" where u.user_id = :userId ").append("\n");
		sql.append(getUserCondition()).append("\n");

		// 定义返回值类型
		BeanPropertyRowMapper<UserInfo> mapper = new BeanPropertyRowMapper<UserInfo>(UserInfo.class);

		// 定义SQL 绑定参数值
		SqlParameterSource param = new BeanPropertySqlParameterSource(query);

		List<UserInfo> list = getSimpleJdbcTemplate().query(sql.toString(),mapper, param);
		
		
		if (null != list && list.size() > 0) {
			user = list.get(0);
		}

		return user;

	}

	/**
	 * 获取用户信息
	 * 
	 * @param request
	 * @param query
	 * @return
	 */
	public UserInfo getUserInfo(UserInfo query) {
		
		if(null == query){
			return null;
		}

		StringBuffer sql = new StringBuffer();

		sql.append(" select ").append("\n");
		sql.append("        u.user_id as user_id, ").append("\n");
		sql.append("        u.login_name, ").append("\n");
		sql.append("        u.display_name, ").append("\n");
		sql.append("        u.password ").append("\n");
		sql.append("  from ").append(TAB_NAME_USER_INFO).append(" u ").append("\n");
		sql.append(" where u.login_name= :loginName ").append("\n");
		sql.append(getUserCondition()).append("\n");

		// 定义返回的bean类型
		// 定义返回值类型
		BeanPropertyRowMapper<UserInfo> mapper = new BeanPropertyRowMapper<UserInfo>(UserInfo.class);

		// 定义SQL 绑定参数值
		SqlParameterSource param = new BeanPropertySqlParameterSource(query);

		// 不能使用 queryForObject ，如果查询到 0 条记录会跑异常
		// org.springframework.dao.EmptyResultDataAccessException
		List<UserInfo> list = getSimpleJdbcTemplate().query(sql.toString(),
				mapper, param);
		UserInfo user = null;
		if (null != list && list.size() > 0) {
			user = list.get(0);
		}

		return user;

	}
	
	
	public UserInfo updateUserInfo(UserInfo query){
		if(null == query){
			return null;
		}
		
		return query;
		
//
//		StringBuffer sql = new StringBuffer();
//
//		sql.append(" update  ").append(TAB_NAME_USER_INFO).append(" u  set ");
//		sql.append("   u.last_login_time = sysdate ");
//		sql.append("  ,u.last_login_ip = :lastLoginIp ");
//		sql.append("  ,u.login_times = u.login_times + 1 ");
//		sql.append(" where u.user_id = :userId ");
//		sql.append(getUserCondition());
//
//		int updateCount = getSimpleJdbcTemplate().update(sql.toString(),
//				new BeanPropertySqlParameterSource(query));
//		
//		return updateCount > 0 ? query : null;
	}

}
